package com.hongxun.modules.test.entity;

import com.hongxun.modules.test.entity.Device;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;



public class CopyFile {

    public static void main(String[] args) {

// 使用adb把手机端文件拷贝到电脑以及把电脑文件拷贝到手机

// 首先获取设备信息

        List<Device> devices = findDevices();

// 遍历设备的id

        for (int i = 0; i < devices.size(); i++) {

// 获取id

            String tempId = devices.get(i).getDeviceId();

            // 使用adb往手机端推送文件

            try {

                // 电脑从手机上拉文件

                Runtime.getRuntime().exec("adb -s " + tempId + " pull sdcard/trace.txt D:\\");

                System.out.println("adb -s " + tempId + " pull sdcard/trace.txt D:\\");

                // 从电脑上推送文件到手机

//                Runtime.getRuntime().exec("adb -s " + tempId + " push F:\\2019.txt sdcard/");
//
//                System.out.println("adb -s " + tempId + " push F:\\2019.txt sdcard/");

                // 把整个文件夹拉到PC端

//                Runtime.getRuntime().exec("adb pull sdcard/temp D:\\");
//
//                System.out.print("adb pull sdcard/temp D:\\");

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

    }


    // 发现手机里的设备


    public static List<Device> findDevices() {

        List<Device> devices = new ArrayList<Device>();

        String str = null;

        int port = 15000;

        Process process = null;

        Device device = null;

        List<String> lines = new ArrayList<String>();

        try {

            process = Runtime.getRuntime().exec("G:/adb/platform-tools/adb.exe devices");

            InputStream in = process.getInputStream();

            BufferedReader read = new BufferedReader(new InputStreamReader(in));

            while ((str = read.readLine()) != null) {

                lines.add(str);

                System.out.println(str);

            }

            for (int i = 1; i < lines.size() - 1; i++) {

                str = lines.get(i);

                String[] deviceStr = str.split("\t");

                if ("device".equals(deviceStr[1])) {

                    device = new Device();

                    device.setDeviceId(deviceStr[0]);

                    device.setState(deviceStr[1]);

                    // 目前先指定port；

                    device.setPort(port);

                    port++;

                    devices.add(device);

                }

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

        return devices;

    }

}